import json
from datetime import datetime

from django.forms.models import model_to_dict
from django.http import JsonResponse
from django.shortcuts import render
from django.template.defaultfilters import title
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods

from zuoye20250916.models import Task


# Create your views here.
@csrf_exempt
@require_http_methods(['POST','GET'])
def get_task(request,id=None):
    if request.method=='GET':
        data = Task.objects.all()
        l = []
        total = len(data)
        title = request.GET.get('title')
        poiority = request.GET.get('poiority')

        if title:
            data = data.filter(title=title)
        if poiority:
            data = data.filter(poiority=poiority)
        for i in data:
            l.append(
                {
                    'id': i.id,
                    'title': i.title,
                    'description': i.description,
                    'deadline': i.deadline,
                    'priority': i.priority,
                    'is_completed': i.is_completed
                }
            )
        return JsonResponse(
            {
                'msg': '查询成功',
                'item': l,
                'total': total
            }
        )
    if request.method=='POST':
        data=json.loads(request.body)
        a=Task.objects.create(
            title=data['title'],
            description=data['description'],
            deadline=datetime.now(),
            priority=data['priority'],
            is_completed=data['is_completed']
        )
        return JsonResponse(
            {
                'msg':'添加成功',
                'id':a.id,
                'title':a.title,
                'description':a.description,
                'deadline':a.deadline,
                'priority':a.priority,
                'is_completed':a.is_completed
            }
        )
@csrf_exempt
@require_http_methods(['GET','PUT','DELETE'])
def put_task(request,id=None):
    if request.method=='PUT':
        data = json.loads(request.body)
        if id == None:
            return JsonResponse(
                {
                    'msg': 'id必填'
                }
            )
        item = Task.objects.get(id=id)
        item.description = data['description']
        item.priority = data['priority']
        return JsonResponse(
            {
                'msg': '修改成功',
                'item': model_to_dict(item)
            }
        )
    if request.method=='GET':
        l=[]
        data = Task.objects.all()
        total=len(data)
        if id:
            data=data.filter(id=id)
        for i in data:
            l.append(
                {
                    'id': i.id,
                    'title': i.title,
                    'description': i.description,
                    'deadline': i.deadline,
                    'priority': i.priority,
                    'is_completed': i.is_completed
                }
            )
        return JsonResponse(
            {
                'msg': '查询成功',
                'item': l,
                'total': total
            }
        )
    if request.method=='DELETE':
        if id==None:
            return JsonResponse(
                {
                    'msg':'id必填'
                }
            )
        if Task.objects.filter(id=id).exists()==False:
            return JsonResponse(
                {
                    'msg':'id错误'
                }
            )
        data=Task.objects.get(id=id).delete()
        return JsonResponse(
            {
                'msg':'删除成功'
            }
        )
@csrf_exempt
def delete_post(request):
    data=json.loads(request.body)
    Task.objects.get(id=data['id']).delete()
    return JsonResponse(
        {
            'msg':'删除成功'
        }
    )
